/*
 * @Author: guozheng 1183320664@qq.com
 * @Date: 2024-04-16 16:00:26
 * @LastEditors: guozheng 1183320664@qq.com
 * @LastEditTime: 2024-04-16 16:01:04
 * @FilePath: \glm_frontend\src\utils\numToChinese.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 阿拉伯数字转汉字
export function numToChinese(num) {
	num = Math.floor(num)
	var chinese = ''
	var digits = Math.floor(Math.log10(num)) + 1
	var digit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
	var times = ['', '十', '百', '千', '万']
	if (digits >= 9) {
		var quotient = Math.floor(num / Math.pow(10, 8))
		var remainder = num % Math.pow(10, 8)
		var remainderDigits = Math.floor(Math.log10(remainder)) + 1
		return toChinese(quotient) + '亿' + (remainderDigits < 8 ? '零' : '') + (remainder > 0 ? toChinese(remainder) : '')
	}
	if (digits == 1) return digit[num]
	if (digits == 2) {
		var quotient = Math.floor(num / 10)
		var remainder = num % 10
		if (quotient > 1) {
			chinese = digit[quotient]
		}
		chinese = chinese + times[1]
		if (remainder > 0) {
			chinese = chinese + digit[remainder]
		}
		return chinese
	}
	if (digits > 5) {
		var quotient = num / Math.pow(10, 4)
		var remainder = num % Math.pow(10, 4)
		var remainderDigits = Math.floor(Math.log10(remainder)) + 1
		return toChinese(quotient) + '万' + (remainderDigits < 4 ? '零' : '') + (remainder > 0 ? toChinese(remainder) : '')
	}
	for (var index = digits; index >= 1; index--) {
		var number = Math.floor((num / Math.pow(10, index - 1)) % 10)
		if (number > 0) {
			chinese = chinese + digit[number] + times[index - 1]
		} else {
			if (index > 1) {
				var nextNumber = Math.floor((num / Math.pow(10, index - 2)) % 10)
				if (nextNumber > 0 && index > 1) {
					chinese = chinese + digit[number]
				}
			}
		}
	}
	return chinese
}
